package com.my.hbase.conf;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class Config {

    private static final Logger logger = LoggerFactory.getLogger(Config.class);

    private static volatile Connection con = null;

    public static Connection getConnection(){
        if (con == null){
            synchronized (Config.class){
                if (con == null){
                    try{
                        Configuration config = HBaseConfiguration.create();
                        con = ConnectionFactory.createConnection(config);
                    }catch (Exception e){
                        logger.error("创建连接失败！",e);
                    }
                }
            }
        }
        return con;
    }

    public static void closeConnection(){
        if (con != null){
            try {
                con.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
